package com.lz.concurrent.aqs;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * ReentrantDemo..
 *
 * DEBUG AQS队列中的节点变化
 *
 * @author Lizhong
 * @date 2020/4/8
 */
public class ReentrantDemo {
    static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    static ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
    static ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();

    public static void main(String[] args) throws InterruptedException {


        // forReadAndWrite();
        // manyLockAndOneUnLock();
        forExclusion();
    }


    private static void forExclusion() {
        for (int i = 0; i < 3; i++) {
            new Thread(new ReentrantDemo.WriteWoeker(), "写线程" + i).start();
        }
    }


    static class WriteWoeker implements Runnable {

        @Override
        public void run() {
            writeLock.lock();

            System.out.println(Thread.currentThread().getName() + "开始了");
            // writeLock.lock();
            // System.out.println(Thread.currentThread().getName() + "写锁第二次"); // 必须要再次释放锁
            writeLock.unlock();
        }
    }
    
}
